/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufrn.dca.graficos;

import java.awt.BasicStroke;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 *
 * @author allan
 */
public class GraficoDegrau extends ApplicationFrame {
    //Series Gráfico 1 - Níveis cm
    XYSeries series2 = new XYSeries("Nível Tanque 2");
        
    List<XYSeries> listaSeries = new ArrayList<XYSeries>(5);
    
    final XYSeriesCollection collection1 = new XYSeriesCollection();

    Color cinzaClaro = new Color(180,180,180);
    Color cinzaEscuro = new Color(70,70,70);
    
    JFreeChart chartPrincipal = createCombinedChart();
    /**
     * Constructs a new demonstration application.
     *
     * @param title  the frame title.
     */
    public GraficoDegrau(final String title) {
        super(title);

        listaSeries.add(series2);
        collection1.addSeries(series2); //Sinal Lido - Nível T2
                
        chartPrincipal.setBackgroundPaint(new Color(77,77,77));
        final ChartPanel panel = new ChartPanel(chartPrincipal, true, true, true, false, true);
        panel.setPreferredSize(new java.awt.Dimension(750, 400));
        setContentPane(panel);
    }

    /**
     * Creates a combined chart.
     *
     * @return the combined chart.
     */
    private JFreeChart createCombinedChart() {
        // create subplot 1...
        final XYDataset data1 = collection1;
        final XYItemRenderer renderer1 = new StandardXYItemRenderer();
        final XYItemRenderer renderer2 = new StandardXYItemRenderer();
        renderer1.setSeriesStroke(0, new BasicStroke(2.0f));
        renderer1.setSeriesStroke(1, new BasicStroke(2.0f));
        renderer1.setSeriesStroke(2, new BasicStroke(2.0f));
        renderer2.setSeriesStroke(0, new BasicStroke(2.0f));
        final NumberAxis rangeAxis1 = new NumberAxis("Nível (cm)");
        rangeAxis1.setAxisLinePaint(cinzaClaro);
        rangeAxis1.setLabelPaint(cinzaClaro);
        rangeAxis1.setTickLabelPaint(cinzaClaro);
        final XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1);
        subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        subplot1.setBackgroundPaint(Color.darkGray);

        /*
        final XYTextAnnotation annotation = new XYTextAnnotation("Hello!", 50.0, 10000.0);
        annotation.setFont(new Font("SansSerif", Font.PLAIN, 9));
        annotation.setRotationAngle(Math.PI / 4.0);
        subplot1.addAnnotation(annotation);
        */        
        
        // parent plot...
        final NumberAxis tempoAxis = new NumberAxis("Tempo (s)");
        tempoAxis.setAxisLinePaint(cinzaClaro);
        tempoAxis.setLabelPaint(cinzaClaro);
        tempoAxis.setTickLabelPaint(cinzaClaro);
        final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(tempoAxis);
        //plot.setRangeMinorGridlineStroke(new BasicStroke(5.0f));
        //plot.setDomainGridlineStroke(new BasicStroke(5.0f));
        
        plot.setGap(10.0);
        // add the subplots...
        plot.add(subplot1, 2);
        plot.setOrientation(PlotOrientation.VERTICAL);

        // return a new chart containing the overlaid plot...
        JFreeChart chart = new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        //chart.getTitle().setPaint(cinzaClaro);
        chart.getLegend().setItemPaint(cinzaClaro);
        chart.getLegend().setBackgroundPaint(cinzaEscuro);
        chart.getLegend().setBorder(new BlockBorder(new Color (50,50,50)));
        return chart;
    }
    
    /**
     * Método usado para adicionar os valores ao grafico
     * @param tempo
     * @param sinalLido1 nivel do tanque 1
     * @param sinalLido2 nivel do tanque 2 
     * @param sinalErro erro
     * @param sinalRef referência 
     * @param sinalEscrito sinal escrito
     */
     public void adicionarValores(double tempo,double sinalLido2) {
         series2.add(tempo, sinalLido2);
     }

     public void atualizarSeries(List<Boolean> lista){
         collection1.removeAllSeries();         
         for (int i = 0; i < lista.size(); i++){
             if (lista.get(i)){
                 if (i < 4){
                     collection1.addSeries(listaSeries.get(i));
                 }                                  
             }
         }
     }    

    public JFreeChart getChartPrincipal() {
        return chartPrincipal;
    }

    public void setChartPrincipal(JFreeChart chartPrincipal) {
        this.chartPrincipal = chartPrincipal;
    }

     
    // ****************************************************************************
    // * JFREECHART DEVELOPER GUIDE                                               *
    // * The JFreeChart Developer Guide, written by David Gilbert, is available   *
    // * to purchase from Object Refinery Limited:                                *
    // *                                                                          *
    // * http://www.object-refinery.com/jfreechart/guide.html                     *
    // *                                                                          *
    // * Sales are used to provide funding for the JFreeChart project - please    *
    // * support us so that we can continue developing free software.             *
    // ****************************************************************************

    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
//    public static void main(final String[] args) {
//
//        final GraficoDegrau demo = new GraficoDegrau("Gráficos");
//        demo.pack();
//        RefineryUtilities.centerFrameOnScreen(demo);
//        demo.setVisible(true);
//
//    }
}
